<?php

class groupController extends baseController
{
	public function show()
	{
		$this->assign('list',	Group::getList());
		$this->display();
	}

	/**
	 * 增加和修改
	 */
	public function save()
	{
		$title	=	fetch_post('title');
		if(!$title) {
			Show::warning("请填写正确的组名");
		}

		$group_id	=	fetch_post('group_id','int');
		if($group_id)
		{
			GroupHelper::modify($group_id, $title);
			Show::ajaxBand(array('method'=>'update'));
		}
		else
		{
			$group_id	=	GroupHelper::add($title);
			if(!$group_id) {
				Show::warning("保存失败，请检查数据重复");
			}
			Show::ajaxBand(array('method'=>'insert'));
		}

		$this->assign('g',	Group::get($group_id));
		$html	=	$this->fetch('group-saved');
		Show::ajaxBand(array(
			'html'		=>	$html,
			'group_id'	=>	$group_id,
		));

		Show::notice("保存成功！");
	}

	/**
	 * 保存排序
	 */
	public function sort()
	{
		if(fetch_get('s'))
		{
			$tmp	=	explode(',', fetch_get('s'));
			foreach($tmp as $k=>$gid) {
				Group::update($gid, array('sort'=>$k));
			}
			Show::notice("保存成功!");
		}
		else {
			Show::error("参数错误");
		}
	}

	/**
	 * 删除
	 */
	public function delete()
	{
		if(!fetch_get('group_id','int')) {
			Show::error("参数错误");
		}
		if(GroupHelper::delete(fetch_get('group_id','int'))) {
			Show::notice("删除成功");
		}
		else {
			Show::warning("删除失败");
		}
	}
	
}

?>
